//
//  MonthStasticsController.m
//  PropertyMananger
//
//  Created by Mansion on 17/3/5.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "MonthStasticsController.h"
#import "StasticsRepairController.h"
#import "StaisticComlainController.h"
#import "SelectedMonthView.h"

@interface MonthStasticsController () <SelectedMonthViewDelegate>

@property (nonatomic,strong) UIButton * selectedTimeBtn;//时间选择
@property (nonatomic,strong) UILabel * timeLabel;//时间
@property (nonatomic,copy) NSString * selectedTimeStr;

@property (nonatomic,assign) NSInteger  selectedMonth;//选择月份
@property (nonatomic,strong) SelectedMonthView * selectedView;
@property (nonatomic,copy) NSString * yearStr;
@property (nonatomic,strong)  NSDateComponents* comp;//系统的时间

@property (nonatomic,strong) StasticsRepairController * publicVC;
@property (nonatomic,strong) StaisticComlainController * complainVC;

@property (nonatomic,assign) NSInteger index;

@end

@implementation MonthStasticsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getYearData];
    
    [self initTitleViewName:@"月工单统计明细"];
    
    [self createSelectedMonthUI];
    
    [self createSegementUI];
    
    
}
- (void)createSegementUI{

    self.publicVC = [[StasticsRepairController alloc]init];
    [self addChildViewController:self.publicVC];
    
    self.complainVC = [[StaisticComlainController alloc]init];
    [self addChildViewController:self.complainVC];
    
    NSArray * childVC = @[self.publicVC,self.complainVC];
    
    NSArray * title_arr = @[@"报修单",@"投诉单"];
    
    self.bottomSView = [[SGSegmentedControlBottomView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.bottomSView.childViewController = childVC;
    self.bottomSView.backgroundColor = [UIColor clearColor];
    self.bottomSView.delegate = self;
    //_bottomView.scrollEnabled = NO;
    [self.view addSubview:self.bottomSView];
    
    self.topSView = [SGSegmentedControlStatic segmentedControlWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) delegate:self childVcTitle:title_arr indicatorIsFull:NO];
    // 必须实现的方法
    [self.topSView SG_setUpSegmentedControlType:^(SGSegmentedControlStaticType *segmentedControlStaticType, NSArray *__autoreleasing *nomalImageArr, NSArray *__autoreleasing *selectedImageArr) {
        
    }];
    self.topSView.selectedIndex = 0;
    [self.view addSubview:self.topSView];


}
- (void)SGSegmentedControlStatic:(SGSegmentedControlStatic *)segmentedControlStatic didSelectTitleAtIndex:(NSInteger)index {
    NSLog(@"index - - %ld", (long)index);
    // 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.bottomSView.contentOffset = CGPointMake(offsetX, 0);
    [self.bottomSView showChildVCViewWithIndex:index outsideVC:self];
    
    self.index = index;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self.bottomSView showChildVCViewWithIndex:index outsideVC:self];
    
    // 2.把对应的标题选中
    [self.topSView changeThePositionOfTheSelectedBtnWithScrollView:scrollView];
}

- (void)createSelectedMonthUI{
    
    [self getYearAndMonthData];
    
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bgView];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    self.selectedTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:self.selectedTimeBtn];
    [self.selectedTimeBtn setImage:IMG(@"ico_statistics_month_white") forState:UIControlStateNormal];
    [self.selectedTimeBtn addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.selectedTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(bgView).with.offset(0);
        make.centerY.equalTo(bgView).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    
    self.timeLabel = [[UILabel alloc]init];
    [bgView addSubview:self.timeLabel];
    
    self.timeLabel.text = self.selectedTimeStr;
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = BHFont(15);
    self.timeLabel.textAlignment= NSTextAlignmentRight;
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(bgView).offset(5);
        make.centerY.equalTo(bgView).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(60, 25));
    }];
    
    
}
- (void)rightButtonClick:(UIButton *)button{
    
    DLog(@"点击选择时间");
    
    //    __block MonthStasticsController *weakSelf = self;
    //    dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerYearMonthMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:1000]];
    //
    //    dateTimePickerView.topViewColor = BHGrayColor;
    //    dateTimePickerView.buttonTitleColor = Theme_Color;
    //    dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
    //        NSLog(@"%@", datetimeStr);
    //
    //        NSArray * timeArr = [datetimeStr componentsSeparatedByString:@"-"];
    //        NSString * timeStr = nil;
    //        if ([timeArr[1]integerValue] <= 9) {
    //
    //            timeStr = [NSString stringWithFormat:@"%@-0%@",timeArr[0],timeArr[1]];
    //        }else{
    //            timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
    //        }
    //
    //        weakSelf.timeLabel.text = [NSString stringWithFormat:@"%@月",timeArr[1]];
    //
    //        NSDictionary * dict = @{@"key":timeStr};
    //
    //        [[NSNotificationCenter defaultCenter]postNotificationName:@"LeaderTimeNotification" object:nil userInfo:dict];
    //
    //        if (weakSelf.index == 0) {
    //
    //            weakSelf.publicVC.isDown = YES;
    //            [weakSelf.publicVC postLeaderOrderRepairData];
    //            DLog(@"重新请求数据");
    //
    //        }else if (weakSelf.index == 1){
    //
    //             weakSelf.complainVC.isDown = YES;
    //            [weakSelf.complainVC postStasticsComplainRepairData];
    //
    //        }
    //
    //    };
    //
    //    if (dateTimePickerView) {
    //        [[UIApplication sharedApplication].keyWindow addSubview:dateTimePickerView];
    //        [dateTimePickerView showHcdDateTimePicker];
    //    }
    
    //时间选择样式二
    self.selectedView = [[SelectedMonthView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 270)/2, self.view.frame.size.height/2-80, 270, 210)];
    [self.view addSubview:self.selectedView];
    
    self.selectedView.delegate = self;
    
    [self.selectedView selectedMonthView];
    
}
- (void)didSelectMonth:(NSInteger)num{
    NSLog(@"点击了 == %ld",(long)num);

    self.selectedMonth = [[NSString stringWithFormat:@"0%ld",num+1]integerValue];
    
    DLog(@"====%ld",self.selectedMonth);

    if (num <9) {
        
        self.yearStr = [NSString stringWithFormat:@"%ld-0%ld",self.comp.year,self.selectedMonth];
    }else{
        
        self.yearStr = [NSString stringWithFormat:@"%ld-%ld",self.comp.year,self.selectedMonth];
    }
    
     self.timeLabel.text = [NSString stringWithFormat:@"%ld月",self.selectedMonth];
    
            NSDictionary * dict = @{@"key":self.yearStr};
    
            [[NSNotificationCenter defaultCenter]postNotificationName:LeaderTimeNoticeCenter object:nil userInfo:dict];
    
    if (self.index == 0) {
        
        self.publicVC.isDown = YES;

        [self.publicVC postLeaderOrderRepairData];
        
        
        self.complainVC.isDown = YES;

        [self.complainVC.tableView reloadData];
    
    }else if (self.index == 1){

        self.publicVC.isDown = YES;

        [self.publicVC postLeaderOrderRepairData];
        
        self.complainVC.isDown = YES;

        [self.complainVC postStasticsComplainRepairData];
        
    }

    DLog(@"==%@",self.yearStr);
    
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSString stringWithFormat:@"%ld",self.selectedMonth] forKey:@"month"];
    [userDefaults synchronize];
    
    
    
}
- (void)getYearData{
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate * dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    self.comp = [gregorian components: unitFlags fromDate:dt];
    
     self.selectedTimeStr = [NSString stringWithFormat:@"%ld月",self.comp.month];
}

#pragma mark - 获取系统时间
- (void)getYearAndMonthData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@"+"];
    DLog(@"日期格式化 == %@",arr[0]);
    
    NSArray * timeArr = [timeDataStr componentsSeparatedByString:@"-"];
    NSString * timeStr = [NSString stringWithFormat:@"%@月",timeArr[1]];
    DLog(@"拼接好的时间===%@",timeStr);
   // self.selectedTimeStr = timeStr;
}
@end
